package com.ruoyi.project.modules.table.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.modules.fieldEnum.domain.FieldEnum;
import com.ruoyi.project.modules.fieldEnum.domain.FieldEnumImport;
import com.ruoyi.project.modules.table.domain.FieldInfo;
import com.ruoyi.project.modules.table.domain.FieldStatusImport;
import com.ruoyi.project.modules.table.mapper.TableInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.table.mapper.FieldStatusMapper;
import com.ruoyi.project.modules.table.domain.FieldStatus;
import com.ruoyi.project.modules.table.service.IFieldStatusService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 字段状态Service业务层处理
 *
 * @author smallrain
 * @date 2021-11-02
 */
@Service
public class FieldStatusServiceImpl implements IFieldStatusService {
    @Resource
    private FieldStatusMapper fieldStatusMapper;
    @Resource
    private TableInfoMapper tableInfoMapper;

    /**
     * 查询字段状态
     *
     * @param id 字段状态主键
     * @return 字段状态
     */
    @Override
    public FieldStatus selectFieldStatusById(Long id) {
        return fieldStatusMapper.selectFieldStatusById(id);
    }

    /**
     * 查询字段状态列表
     *
     * @param fieldStatus 字段状态
     * @return 字段状态
     */
    @Override
    public List<FieldStatus> selectFieldStatusList(FieldStatus fieldStatus) {
        return fieldStatusMapper.selectFieldStatusList(fieldStatus);
    }

    /**
     * 新增字段状态
     *
     * @param fieldStatus 字段状态
     * @return 结果
     */
    @Override
    public int insertFieldStatus(FieldStatus fieldStatus) {
        fieldStatus.setCreateTime(DateUtils.getNowDate());
        return fieldStatusMapper.insertFieldStatus(fieldStatus);
    }

    /**
     * 修改字段状态
     *
     * @param fieldStatus 字段状态
     * @return 结果
     */
    @Override
    public int updateFieldStatus(FieldStatus fieldStatus) {
        fieldStatus.setUpdateTime(DateUtils.getNowDate());
        return fieldStatusMapper.updateFieldStatus(fieldStatus);
    }

    /**
     * 批量删除字段状态
     *
     * @param ids 需要删除的字段状态主键
     * @return 结果
     */
    @Override
    public int deleteFieldStatusByIds(String ids) {
        return fieldStatusMapper.deleteFieldStatusByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除字段状态信息
     *
     * @param id 字段状态主键
     * @return 结果
     */
    @Override
    public int deleteFieldStatusById(Long id) {
        return fieldStatusMapper.deleteFieldStatusById(id);
    }

    @Override
    public List<FieldStatus> fieldList() {
        return fieldStatusMapper.fieldList();
    }

    @Override
    public String importData(List<FieldStatusImport> fieldStatusImportList) {
        Map<String, Long> cache = new HashMap<>();
        Set<String> errTips = new HashSet<>();
        int successNum = 0, errorNum = 0;
        if (CollectionUtil.isNotEmpty(fieldStatusImportList)) {
            for (FieldStatusImport fieldEnumImport : fieldStatusImportList) {
                Long fieldId = cache.get(fieldEnumImport.getTableEnName() + fieldEnumImport.getFieldEnName());
                if (fieldId == null) {
                    FieldInfo fieldInfo = this.tableInfoMapper.selectFieldInfo(fieldEnumImport.getTableEnName(), fieldEnumImport.getFieldEnName());
                    if (fieldInfo != null && fieldInfo.getIsFieldStatus().equals("Y")) {
                        fieldId = fieldInfo.getId();
                        if (fieldId != null) {
                            cache.put(fieldEnumImport.getTableEnName() + fieldEnumImport.getFieldEnName(), fieldId);
                        }
                    }
                }
                if (fieldId != null) {
                    FieldStatus fieldStatus = new FieldStatus();
                    BeanUtils.copyProperties(fieldEnumImport, fieldStatus);
                    fieldStatus.setFieldId(fieldId);
                    this.fieldStatusMapper.insertFieldStatus(fieldStatus);
                    successNum ++;
                } else {
                    errorNum ++;
                    errTips.add(fieldEnumImport.getTableEnName() + "-"+ fieldEnumImport.getFieldEnName());
                }
            }
            if (errTips.size() > 0) {
                return "导入成功" + successNum + "条, 错误："+errorNum+"条，错误提醒：" + JSONObject.toJSONString(errTips);
            }
            return "导入成功" + successNum + "条";
        }
        return "导入失败";
    }
}
